import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {Message} from 'element-ui';
import auth from "./auth";
NProgress.configure({
    showSpinner: false,
    ease: 'ease',
    speed: 500
})
const request = axios.create({
    baseURL: '/api',
    timeout: 12000,
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json"
    },
    withCredentials:true
})
export default request
request.interceptors.request.use(config => {
    // Do something before request is sent
    NProgress.start()
    return config
}, error => {
    // Do something with request error
    return Promise.reject(error);
});
request.interceptors.response.use(response => {
    NProgress.done()
    if (response.data.flag == true) {
        if (response.data.message != "" && response.data.message != null) {
            Message({
                message: response.data.message,
                type: 'success'
            });
        }
        return response.data;
    } else {
        if(response.data.code== 401)
        {
            auth.removeToken()
            // location.reload()
            return Promise.reject(response);
        }
            Message({
                message: response.data.message,
                type: 'error'
            });
        return Promise.reject(response);
    }
}, error => {
    NProgress.done()
    return Promise.reject(error);
});